<?php
/**
 * 客诉退货汇总
 */
namespace App\Logic\Quality;


use App\Models\Amazon\FbaCustomerReturn;
use App\Models\Quality\QComplaint;
use App\Models\Quality\QComplaintTotal;
use Illuminate\Support\Facades\DB;
class QualityTotal
{
    /**
     * 客诉质量汇总
     */
    public static function totalQuality($date = '2023-09'){

        if (!$date) {
            $date = date('Y-m', strtotime('-1 month'));
        }
        $startTime = date('Y-m-01 00:00:00', strtotime($date));
        $endTime = date('Y-m-t 23:59:59', strtotime($startTime));

        //退货单
        FbaCustomerReturn::query()
            ->distinct('order_no')
            ->whereBetween('purchase_date', [$startTime, $endTime])
            ->chunkById(500, function ($items){
                DB::beginTransaction();
                try{
                    foreach ($items as $item){
                        //客诉汇总表
                        $flag1 = QComplaintTotal::query()->where('type', QComplaintTotal::TYPE_RETRUN)->updateOrCreate(
                            ['order_no' => $item->order_no],
                            [
                                'origin' => 10, //退货
                                'origin_id' => $item->id,
                                'type' => QComplaintTotal::TYPE_RETRUN, //退货表
                                'report_date' => $item->report_date ?? 0,
                                'area_id' => $item->area_id ?? 0,
                                'country_id' => $item->country_id ?? 0,
                                'psku_id' => $item->psku_id ?? 0,
                                'seller_sku' => $item->seller_sku ?? '',
                                'asin' => $item->asin ?? '',
                                'classify_id' => $item->classify_id ?? 0,
                                'content' => $item->content ?? '',
                                'tag' => $item->tag ?? '',
                            ]
                        );
                        if(!$flag1){
                            DB::rollBack();
                            continue;
                        }
                    }
                    DB::commit();
                }catch (\Exception $e){
                    DB::rollBack();
                    report($e);
                }
            });

        sleep(2);
        //客诉单
        QComplaint::query()
            ->whereBetween('report_date', [$startTime, $endTime])
            ->chunkById(500, function ($items) use($startTime, $endTime){
                DB::beginTransaction();
                try{
                    foreach ($items as $item){
                        if ($item->order_no) {
                            //有订单号
                            $flag1 = QComplaintTotal::query()->where('type', QComplaintTotal::TYPE_COMPLAINT)->updateOrCreate(
                                ['order_no' => $item->order_no],
                                [
                                    'origin' => $item->origin,
                                    'origin_id' => $item->id,
                                    'type' => QComplaintTotal::TYPE_COMPLAINT, //客诉表
                                    'report_date' => $item->report_date ?? 0,
                                    'area_id' => $item->area_id ?? 0,
                                    'country_id' => $item->country_id ?? 0,
                                    'psku_id' => $item->psku_id ?? 0,
                                    'seller_sku' => $item->seller_sku ?? '',
                                    'asin' => $item->asin ?? '',
                                    'classify_id' => $item->classify_id ?? 0,
                                    'content' => $item->content ?? '',
                                    'tag' => $item->tag ?? '',
                                    'pics' => $item->image->pics ?? '',
                                ]
                            );
                        }else{
                            $isExsit = QComplaintTotal::query()
                                ->where('origin_id', $item->id)
                                ->where('type', QComplaintTotal::TYPE_COMPLAINT)
                                ->whereBetween('purchase_date', [$startTime, $endTime])
                                ->first(); //来源id
                            if ($isExsit) continue;
                            //无订单号
                            $flag1 = QComplaintTotal::query()->create([
                                'order_no' => '',
                                'origin' => $item->origin,
                                'origin_id' => $item->id,
                                'type' => QComplaintTotal::TYPE_COMPLAINT, //客诉表
                                'report_date' => $item->report_date ?? 0,
                                'area_id' => $item->area_id ?? 0,
                                'country_id' => $item->country_id ?? 0,
                                'psku_id' => $item->psku_id ?? 0,
                                'seller_sku' => $item->seller_sku ?? '',
                                'asin' => $item->asin ?? '',
                                'classify_id' => $item->classify_id ?? 0,
                                'content' => $item->content ?? '',
                                'tag' => $item->tag ?? '',
                                'pics' => $item->image->pics ?? '',
                            ]);
                        }
                        if(!$flag1){
                            DB::rollBack();
                            continue;
                        }
                    }
                    DB::commit();
                }catch (\Exception $e){
                    DB::rollBack();
                    report($e);
                }
            });
    }

}
